package com.scs.application.modules.dept.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.dept.entity.BudgetItem;
import com.scs.application.modules.dept.entity.BudgetMatr;
import com.scs.application.modules.dept.mapper.BudgetItemMapper;
import com.scs.application.modules.dept.mapper.BudgetMatrMapper;
import com.scs.application.modules.dept.service.BudgetItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class BudgetItemServiceImpl extends BaseServiceImpl<BudgetItemMapper, BudgetItem> implements BudgetItemService {

    @Autowired
    private BudgetMatrMapper budgetMatrMapper;


    @Override
    public boolean removeCascadeByIds(Collection<? extends Serializable> idList) {
        // applyItemMapper.delete(Wrappers.<ApplyItem>query().in("apply_id", idList));

        for (Serializable id : idList) {
            List<BudgetMatr> budgetMatrList = budgetMatrMapper.selectList(new QueryWrapper<BudgetMatr>().eq("budget_item_id",id));
            if (budgetMatrList != null && budgetMatrList.size() > 0) {
                List<String> itemIdList = budgetMatrList.stream().map(BudgetMatr::getId).collect(Collectors.toList());
                budgetMatrMapper.deleteBatchIds(itemIdList);
            }
        }
        return super.removeCascadeByIds(idList);
    }

    @Override
    public boolean removeCascadeById(Serializable id) {
        this.removeCascadeByIds(Arrays.asList(id));
        return true;
    }
}
